<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class GenerateUserCount implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $user_id;
    private $member_id;
    private $start_time;
    private $end_time;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_id,$member_id, $start_time, $end_time)
    {
        //
        $this->user_id = $user_id;
        $this->member_id = $member_id;
        $this->start_time = $start_time;
        $this->end_time = $end_time;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

            $subQueryA = DB::table("member")
            ->where('source_member', '=', $this->member_id)
            ->pluck('member_id');
            // $itme['promotion_num']=
            // DB::table("member")
            // ->where([ [ 'source_member', '=',  $this->member_id ] ,[ 'reg_time', '>',  $this->start_time],[ 'reg_time', '<',  $this->end_time]])->count()
            // +
            // Db::table("member")
            // ->whereIn('source_member', $subQueryA)
            // ->where([[ 'reg_time', '>',  $this->start_time],[ 'reg_time', '<',  $this->end_time]])->count();
            $subQueryB = DB::table("order")->where('order_status', '>', 0)->where('order_money', '>', 19)->where([ [ 'create_time', '>',  $this->start_time],[ 'create_time', '<',  $this->end_time]])->pluck('member_id');
            // $direct_order = DB::table("member")->whereIn('member_id', $subQueryB)->where('source_member', '=',  $this->member_id)->count();
            // $indirect_order = DB::table("member")->whereIn('source_member',  $subQueryA)->whereIn('member_id', $subQueryB)->count();
            // $order_num=$direct_order+$indirect_order;
            // $subQueryC = DB::table("zeai_tg_user")->where('tguid', '=', $this->user_id)->pluck('id');
            // $direct_money=0;
            // $indirect_money=0;
            // $agent_money=$direct_money+$indirect_money;
            // $item['order_num']=$order_num;
            // $item['agent_money']=$agent_money;
            Redis::hset('user-'.$this->user_id,'direct_num',DB::table("member")->where([ [ 'source_member', '=',  $this->member_id] ,[ 'reg_time', '>',  $this->start_time],[ 'reg_time', '<',  $this->end_time],[ 'mobile', '<>',  '']])->count());
            Redis::hset('user-'.$this->user_id,'indirect_mobile_num',Db::table("member")
            ->whereIn('source_member', $subQueryA)
            ->where(
                [
                    [ 'reg_time', '>',  $this->start_time],[ 'reg_time', '<',  $this->end_time],[ 'mobile', '<>',  '']
                ]
            )->count());
            Redis::hset('user-'.$this->user_id,'promotion_num',DB::table("member")
            ->where([ [ 'source_member', '=',  $this->member_id ] ,[ 'reg_time', '>',  $this->start_time],[ 'reg_time', '<',  $this->end_time]])->count()
            +
            Db::table("member")
            ->whereIn('source_member', $subQueryA)
            ->where([[ 'reg_time', '>',  $this->start_time],[ 'reg_time', '<',  $this->end_time]])->count());
            Redis::hset('user-'.$this->user_id,'order_num',DB::table("member")->whereIn('member_id', $subQueryB)->where('source_member', '=',  $this->member_id)->count()+DB::table("member")->whereIn('source_member',  $subQueryA)->whereIn('member_id', $subQueryB)->count());
            echo "================".PHP_EOL;
            echo json_encode(Redis::hvals('user-'.$this->user_id)).PHP_EOL;
            echo 'lpop='.Redis::lpop("userids").PHP_EOL;
            echo 'llen='.Redis::llen("userids").PHP_EOL;
            echo "================".PHP_EOL;
    }
}
